<?php defined('SYSPATH') or die('No direct script access.');
/**
* Module Products Grouping (Categories, Subcategories, and Brands)
*/
class Module_Products_ProductGroups {
	
	private $controller;
	private $ss;
	private $thesystems;
	private $myfunctions;
	private $message;
	
	static function &factory() {
	   $o = new Module_Products_ProductGroups();
	  return $o;	
	}
	
	public function __construct()
	{
		$this->controller  = Request::current()->controller();
		$this->ss          = Session::instance();
		$this->thesystems  = Helper_theSystems::factory();
		$this->myfunctions = Helper_myFunctions::factory();
		$this->message     = Language_Message::factory();
	}
	
	public function productgroups($views, $ids, $command, $id)
	{
		$rpp				= $this->thesystems->get_config('cms', 'pagination_medium_view');  // Record per page
		$halaman		= Arr::get($_GET,'page',1);
		$offset			= ($halaman - 1)*$rpp;
		$groups			= NULL;
		
		switch ($views)
		{
		case "Categories":
					$table	= 'prodcategories';
					break;
		
		case "Brands":
					$table	= 'prodbrands';
					break;
		
		default:
					$table	= 'prodcategories';
					break;
		}
		
		switch ($command)
		{
		case "group_create":
				  // Create New Product Groups
				  if(!is_null($groups))
				  {
						$group_id			= $_POST['group_id'];
				  }
				  
					$position    = $_POST['position'];
					$value       = $_POST['value'];
					$description = $_POST['description'];
					$approve     = Arr::get($_POST,('approve'));
					$code        = Arr::get($_POST,('code'));
				  
				  if(!is_null($groups))
				  {
						$query = DB::insert($table, array($groups.'_id','position','value','description','code','approve'))
						  ->values(array($group_id,$position,$value,$description,$code,$approve));
				  } else {
						$query = DB::insert($table, array('position','value','description','approve'))
						  ->values(array($position,$value,$description,$approve));
				  }
					
					$result = $query->execute();
					if ($result[1] == 1) {
						$this->message->store_message('c_success');
					  $r = Request::current();
					  $r->redirect($this->controller.'/productgroups?views='.$views.'&ids='.$ids.'&id='.$result[0]);
					} else {
						$this->message->store_message('c_fails');
					}
					break;
		
		case "group_update":
				  // Update Product Group
					$p['position']    = $_POST['position'];
					$p['value']       = $_POST['value'];
					$p['description'] = $_POST['description'];
					$p['approve']     = Arr::get($_POST,('approve'));
					$p['code']        = Arr::get($_POST,('code'));
				  
				  $query = DB::update($table)
					  ->set($p)
					  ->where('id', '=', $id);
				  $result = $query->execute();
					  if ($result == 1) {
							$this->message->store_message('u_success');
							$r = Request::current();
							$r->redirect($this->controller.'/productgroups?views='.$views.'&ids='.$ids.'&id='.$id);
					  } else {
							$this->message->store_message('u_fails');
					  }
					break;
		
		case "group_delete":
					// Delete Product Group Data
				  $query = DB::delete($table)
					  ->where('id', '=', $id)
					  ->limit(1);
				  $result = $query->execute();
					  if ($result == 1) {
							$this->message->store_message('d_success');
							$r = Request::current();
							$r->redirect($this->controller.'/productgroups?views='.$views.'&ids='.$ids);
					  } else {
							$this->message->store_message('d_fails');
					  }
					break;
		}
		
		$all_items = DB::select()
			->from($table);
		
		if(!is_null($groups))
		{
			$all_items = $all_items->where($groups.'_id', '=', $ids);
		}
		
		$all_items = $all_items->execute();
		$count = $all_items->count();
		
		$productgroup = DB::select()
			->from($table)
			->limit($rpp)
			->offset($offset);
		
		if(!is_null($groups))
		{
			$productgroup = $productgroup->where($groups.'_id', '=', $ids);
		}
		
		$productgroup = $productgroup->order_by('position', 'ASC')
			->order_by('value', 'ASC');
		$productgroups = $productgroup->execute();
		
		$productgroupupdate = DB::select()
			->from($table)
			->where('id', '=', $id)
			->limit(1)
			->execute();
		
		$pagination = Helper_myFunctions::factory()->paginations($count,$rpp);
		
		$content['title']		= 'Product '.$views;
		$content['content']	= View::factory('inc/productgroups')
			->set('productgroups',$productgroups)
			->set('productgroupupdate',$productgroupupdate[0])
			->set('pagination',$pagination)
			->render();
		
		return $content;
	}
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
}